
jBGallery.EventTimer = function(el, arr, ms, callback, cancel, debug){
    $.each(arr, function(i,v){
        var _timer = null;
        $(el).bind(v, function(ev) {
            if (_timer) {
                clearTimeout(_timer);
            }
            _timer = setTimeout(function(){
                callback(ev);
            }, ms);
        });
        if(cancel){
            $(el).bind(cancel, function(ev) {
                if(_timer)clearTimeout(_timer);
            });
        }
    });
};

jBGallery.AutoToggle = function(menu, options, effects){

    function _on(){
        _set_interval();
        _toggle_menu(effects[0])
    }
    function _off(){
        _toggle_menu(effects[1]);
    }
    function _toggle_menu(method){
        $(menu).each(function(){
            if($(this).css("visibility") !== "hidden")
                $(this)[method](options.timers.fade);
        });
    }
    function _set_interval(){
        if(_interval){
            clearInterval(_interval);
            _interval = false;
        }
        _interval = setInterval(_off, options.timers.autohide);
    }
    var _interval = false;
    var _timer = false;
    if(!effects)
        effects = ["slideDown","slideUp"];

    $(document)
    .bind("click.jbgallery", _on)
    .bind("mousemove.jbgallery", function(){
        if(_timer){
            clearTimeout(_timer);
            _timer = false;
        }
        _timer = setTimeout(_on, options.delays.mousemove);
    });
    _set_interval();
    
};
jBGallery.Randomizer = function(current, length){
  // RANDOM LOAD - blackout: http://www.grayhats.org
  // 2011/05/16 fixed bug - thanks to http://grethegrundahl.dk/
    current = Math.ceil((length-1)*Math.random());
    if(current >= length)// || current == 0)
        current = 0;
    return current;
};